#!/bin/bash

echo "Enter MySQL new username:"
read username

echo "Enter MySQL user's password:"
read -s password

echo "Enter MySQL user's host (default: localhost):"
read host

# 如果未提供主机，则将主机设置为默认值 localhost
if [ -z "$host" ]; then
  host="localhost"
fi

# 创建MySQL用户
echo "Enter the root password"
mysql -u root -p -e "CREATE USER '$username'@'$host' IDENTIFIED BY '$password';"
echo "MySQL user '$username' created successfully."

# 给MySQL用户授权
echo "Enter the root password"
mysql -u root -p -e "grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER on *.* to '$username'@'$host';flush privileges;"
echo "MySQL user '$username' granted SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER  successfully."

